<template>
  <div v-if="topPic.length > 0">
    <div class="header">
      <router-link to="/search" class="search-box">
        <!-- <div class="logo"><img width="24" height="24" src="../assets/images/logo.png"></div> -->
        <div class="search-link">
          <x-icon class="search-icon" type="ios-search" size="20"></x-icon>
          <span>请输入要搜索的商品</span>
        </div>
      </router-link>
      
    </div>
    <!-- 首页滚动大图 -->
    <b-swiper :pic="topPic" padd-bom="40%" class="banner"></b-swiper>
    <!-- 快速入口 -->
    <ul class="quickly-menu clearfix">
      <router-link class="quickly-itme" :to="{ path: '/category' }" tag="li">
        <i><img src="./../assets/images/icon_01.png"></i>
        <h3>全部分类</h3>
      </router-link>
      <router-link class="quickly-itme" :to="{ path: '/cart' }" tag="li">
        <i><img src="./../assets/images/icon_02.png"></i>
        <h3>购物车</h3>
      </router-link>
      <router-link class="quickly-itme" :to="{ path: '/my/order/', query: { type: 0 } }" tag="li">
        <i><img src="./../assets/images/icon_03.png"></i>
        <h3>我的订单</h3>
      </router-link>
      <router-link class="quickly-itme" :to="{ path: '/seckill' }" tag="li">
        <i><img src="./../assets/images/icon_04.png"></i>
        <h3>限时秒杀</h3>
      </router-link>

    </ul>
    <split></split>
    <div class="seckill">
      <div class="seckill-title">
        <div class="seckill-title-left">
          <img src="../assets/images/skill.png" height="27">
          <clocker v-if="timeArr.length > 0" class="seckill-time" :time="timeArr[1].date">
            <span>%H</span> : <span>%M</span> : <span>%S</span>
          </clocker>
        </div>
        <router-link class="seckill-more" to="/seckill">
          <span>限时限量</span>
          <x-icon type="ios-arrow-right" size="19"></x-icon>
        </router-link>
      </div>
      <swiper class="seckill-scroll bottom-1px" :options="scrollSwiperOption">
        <swiper-slide class="seckill-item" v-for="item in seckillList" :key="item.id">
          <div class="seckill-item-img">
            <router-link :to="{ path: '/goods/detail',  query: { id: item.id } }" class="seckill-item-link img-holder">
              <img class="obj-cover" :src="item.imgSrc">
            </router-link>
          </div>
          <div class="seckill-item-price">
            <span class="price"><i>¥</i>{{ item.price | toDecimal }}</span>
            <span class="old-price"><i>¥</i>{{ item.oldPrice | toDecimal }}</span>
          </div>
        </swiper-slide>
      </swiper>
    </div>
    <!-- 特色推荐 -->
    <split></split>
    <div class="floor">
      <div class="floor-title bottom-1px">
        <h2>为你推荐</h2>
        <span>RECOMMENDED</span>
      </div>
      <div>
        <ul 
        class="goods-list"
        v-infinite-scroll="loadMore"
        infinite-scroll-disabled="loading"
        infinite-scroll-distance="10">
          <router-link :to="{ path: '/goods/detail', query: { id: item._id } }" class="goods-item" v-for="(item,index) in goodsList" :key="index" :class="{ p_left:index%2 != 0, p_right:index%2 == 0 }" tag="li" @click.stop.prevent="addBrowse(item)">
            <div class="goods-item-link">
              <div class="goods-item-img">
                <span class="img-holder"><img class="obj-cover" :src="item.goodsCover"></span>
              </div>
              <div class="goods-product-text">{{ item.goodsName }}</div>
              <p class="goods-product-info">
                <span class="goods-product-price">
                  <i>¥</i>
                  <span v-if="!item.seckillPrice || !item.isSeckill" class="big-price">{{ item.price | toDecimal }}</span>
                  <span v-else class="big-price">{{ item.seckillPrice | toDecimal }}</span>
                </span>
                <router-link :to="{ path: '/goods', query: { cid: item.categoryId } }" tag="span" class="guess-button">看相似</router-link>
              </p>
            </div>
          </router-link>
        </ul>
        <div class="spinner-wrapper">
          <mt-spinner v-show="loading" class="triple-bounce" type="triple-bounce"></mt-spinner>
          <load-more v-show="nomore" :show-loading="false" tip="暂无更多数据" background-color="#f5f5f5"></load-more>
        </div>
      </div>
    </div>
    <!-- 底部导航 -->
    <footer-nav bottom-nav="首页" bg-color="#fff" text-color="#999" :position="{ position: 'fixed' }"></footer-nav>
  </div>
</template>

<script>  
import { Clocker, ChinaAddressV4Data, LoadMore } from 'vux'  
import { mapGetters, mapActions } from 'vuex'
import { getNowTime, toDecimal } from '@/utils/mUtils'
import http from '@/utils/http'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import BSwiper from '@/components/public/PicSwiper'
import FooterNav from '@/components/FooterNav'
import Split from '@/components/public/Split'

export default {
  components: {
    BSwiper,
    FooterNav,
    Split,
    swiper,
    swiperSlide,
    Clocker,
    LoadMore
  },
  data () {
    return {
      loading: true,
      pageNow: 1,
      limit: 4,
      nomore: false,
      scrollSwiperOption: {
        slidesPerView: 'auto',
        spaceBetween: 0,
        freeMode: true
      },
      timeArr: [],
      isShowBtn: true,
      scroll: '',
      goodsList: [],
      seckillList: [],
      topPic: [],
      list: []
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    ...mapActions(['addBrowse']),
    _getListData () {
      // params提交的数据
      let params = {
        page: this.pageNow, // 当前第几页
        limit: this.limit // 一次取几条数据
      }
      http.get('/api/v1/getGoods', params, false, res => {
        if (res.status == 1 && res.data.length > 0) {
          this.$nextTick(() => {
            for (let i = 0; i<res.data.length; i++) {
              this.goodsList.push(res.data[i])
            }
            this.pageNow++
            this.nomore = false
            this.loading = false
          })
        }else {
          this.loading = false
          this.nomore = true
          return
        }
      })
    },
    loadMore () {
      if (!this.nomore) {
        this.loading = true
        setTimeout(() => {
          this._getListData()
        }, 500)
      }
    }
  },
  mounted () {
    // 初次加载数据
    this.timeArr = getNowTime() 
    http.get('/api/v1/getAdvList', {}, false, res => {
      this.$nextTick(() => {
         this.topPic = res.data
      })
    })

    http.get('/api/seckillList', {}, false, res => {
      this.$nextTick(() => {
        this.seckillList = res.data
      })
    })      
    this._getListData()
    this.$nextTick(() => {
      window.addEventListener('scroll', this.getScrollTop)
    })
  },
  filters: {
    toDecimal (val) {
      return toDecimal(val)
    }
  }
}
</script>

<style scoped>
.header {
  position: fixed;
  width: 100%;
  max-width: 640px;
  top: 0;
  display: flex;
  padding: 8px 15px;
  z-index: 99;
  align-items: center;
}
.hd-trapent {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  z-index: 96;
  opacity: 0;
  background: #f23030;
}
.search-box {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, .85);
  padding: 5px 10px;
  border-radius: 30px;
  z-index: 98;
  box-shadow: 0 2px 5px rgba(0, 0, 0, .2);
}
.logo {
  margin-right: 8px;
}
.search-icon {
  fill: #999;
}
.search-link {
  font-size: 0;
}
.search-link * {
  display: inline-block;
  vertical-align: middle;
}
.search-link span {
  font-size: 12px;
  padding-left: 5px;
  color: #999;
}
.to-login,
.to-my {
  position: relative;
  display: block;
  font-size: 14px;
  color: #fff;
  margin-left: 15px;
  z-index: 97;
}
.to-my {
  font-size: 20px;
}
.quickly-menu {
  padding: 10px 0;
  background: #fff;
}
.quickly-itme {
  width: 25%;
  float: left;
  padding: 6px 0;
  text-align: center;
}
.quickly-itme i {
  display: inline-block;
  width: 70%;
  max-width: 60px;
  border-radius: 100%;
}
.quickly-itme i img {
  width: 100%;
  height: 100%;
}
.quickly-itme h3 {
  font-size: 13px;
  line-height: 13px;
  padding-top: 3px;
  color: #333;
}
.seckill {
  background: #fff;
}
.seckill-title {
  display: flex;
  padding: 3px 10px;
}
.seckill-title-left {
  flex: 1;
  align-items: center;
}
.seckill-title-left strong,
.seckill-title-left img {
  display: inline-block;
  vertical-align: middle;
}
.seckill-title-left strong {
  position: relative;
  margin-top: 3px;
}
.seckill-title-left strong {
  font-size: 12px;
  color: #000;
}
.seckill-title-left span {
  font-size: 13px;
}
.seckill-more {
  align-items: center;
  flex: 0 0 auto;
}
.seckill-more * {
  display: inline-block;
  vertical-align: middle;
  line-height: 27px;
}
.seckill-more span {
  font-size: 12px;
  color: #999;
}
.seckill-more svg {
  fill: #999;
}
.seckill-item {
  max-width: 108px;
}
.seckill-item-img {
  padding: 0 11px;
}
.seckill-item-link {
  padding-bottom: 100%;
}
.seckill-item-price {
  padding-top: 10px;
  text-align: center;
}
.seckill-item-price .price {
  font-size: 16px;
  line-height: 16px;
  height: 16px;
  color: #f23030;
  font-weight: bold;
}
.seckill-item-price span {
  display: block;
}
.seckill-item-price .price i {
  display: inline-block;
  font-size: 11px;
  padding-right: 2px;
}
.seckill-item-price .old-price {
  font-size: 12px;
  color: #686868;
  text-decoration: line-through;
  padding-bottom: 10px;
}
.title-img {
  display: block;
  text-align: center;
}
.title-img img {
  display: inline;
}
.horizontal-view {
  position: relative;
  display: flex;
}
.horizontal-img {
  flex: 1;
}
.t_Line {
  flex: 0 0 auto;
  width: 1px; 
  height: 100%;
  background-color: rgb(232, 232, 232);
}
.l_Line {
  width: 100%;
  height: 1px;
  background-color: rgb(232, 232, 232);
}
.seckill-time {
  display: inline-block;
  vertical-align: middle;
  margin-left: 10px;
  font-family: "Microsoft YaHei";
}
.seckill-time span {
  font-size: 12px;
  display: inline-block;
  position: relative;
  padding: 0 3px;
  color: #333;
}
.seckill-time span:after {
  -webkit-border-radius: 2px;
  border-radius: 3px;
  height: 200%;
  content: '';
  width: 200%;
  border: 1px solid #dfdfdf;
  position: absolute;
  top: -1px;
  left: -1px;
  transform: scale(0.5, 0.5);
  -webkit-transform: scale(0.5, 0.5);
  transform-origin: left top;
  -webkit-transform-origin: left top;
  z-index: 10;
}
.goods-list {
  overflow: hidden;
}
.goods-item {
  width: 50%;
  float: left;
}
.goods-item {
  padding-bottom: 4px;
}
.goods-item-link {
  display: block;
  background: #fff;
}
.img-holder {
  padding-bottom: 100%;
}
.p_left {
  padding-left: 2px;
}
.p_right {
  padding-right: 2px;
}
.goods-product-text {
  color: #232326;
  font-size: 13px;
  padding: 8px 5px 0 5px;
  display: block;
  height: 40px;
  line-height: 16px;
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.goods-product-info {
  padding: 0 5px;
  display: flex;
  align-items: center;
  padding-bottom: 5px;
}
.goods-product-price {
  flex: 1;
  color: #f23030;
  font-size: 0;
} 
.goods-product-price i {
  display: inline-block;
  font-size: 13px;
  padding-right: 2px;
}
.big-price {
  font-size: 16px;
}
.guess-button {
  flex: 0 0 auto;
  align-items: center;
  text-align: center;
  color: #686868;
  font-size: 10px;
  width: 49px;
  height: 22px;
  line-height: 22px;
  border: 1px solid #d1d1d1;
}
.floor-title {
  padding: 10px 0;
  background: #fff;
  text-align: center;
  margin-bottom: 2px;
}
.floor-title h2,
.floor-title span {
  line-height: normal;
}
.floor-title h2 {
  font-size: 20px;
  font-weight: 700;
  color: #000;
}
.floor-title span {
  color: #ccc;
  font-size: 10px;
  letter-spacing: 2px;
  display: block;
}
</style>
